//------------------------------------------------------------------------------
// <copyright file="CustomMessageBox.xaml.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DAnton.Utilities.Extensions;
using System;

namespace DotBeer.UI.WPFClient
{
    /// <summary>
    /// Interaction logic for CustomMessageBox.xaml
    /// </summary>
    public partial class CustomMessageBox : Window
    {
        #region private members

        bool optionsChoosen = false;
        Style buttonLightStyle;
        MessageBoxResult result = MessageBoxResult.OK;

        #endregion

        #region constructors

        private CustomMessageBox(string title, string message, MessageBoxButton buttons)
        {
            this.InitializeComponent();

            this.buttonLightStyle = (Style)FindResource("ButtonLightStyle");

            this.TextBlockTitle.Text = title;
            this.TextBlockMessage.Text = message;
            this.AddButtons(buttons);

            this.SetOwner();
        }

        #endregion

        #region properties

        public MessageBoxResult Result
        {
            get { return this.result; }
        }

        #endregion

        #region public methods

        public static MessageBoxResult Show(string message)
        {
            CustomMessageBox msg = new CustomMessageBox("", message, MessageBoxButton.OK);
            msg.ShowDialog();
            return msg.Result;
        }

        public static MessageBoxResult Show(string message, string title)
        {
            CustomMessageBox msg = new CustomMessageBox(title, message, MessageBoxButton.OK);
            msg.ShowDialog();
            return msg.Result;
        }

        public static MessageBoxResult Show(string message, string title, MessageBoxButton buttons)
        {
            CustomMessageBox msg = new CustomMessageBox(title, message, buttons);
            msg.ShowDialog();
            return msg.Result;
        }

        #endregion

        #region private methods

        private void AddButtons(MessageBoxButton buttons)
        {
            switch (buttons)
            {
                case MessageBoxButton.OK:
                    this.AddOkButton();
                    break;
                case MessageBoxButton.YesNo:
                    this.AddYesNoButton();
                    break;
            }
        }

        private void AddOkButton()
        {
            Button button = this.GetButton("OK");
            button.Click += delegate 
            {
                this.result = MessageBoxResult.OK;
                this.optionsChoosen = true;
                this.Close();
            };

            this.StackPanelButtons.Children.Add(button);
            button.Focus();
        }

        private void AddYesNoButton()
        {
            Button buttonYes = this.GetButton("Yes");
            buttonYes.Click += delegate
            {
                this.result = MessageBoxResult.Yes;
                this.optionsChoosen = true;
                this.Close();
            };

            Button buttonNo = this.GetButton("No");
            buttonNo.Click += delegate
            {
                this.result = MessageBoxResult.No;
                this.optionsChoosen = true;
                this.Close();
            };

            this.StackPanelButtons.Children.Add(buttonYes);
            this.StackPanelButtons.Children.Add(buttonNo);

            buttonYes.Focus();
        }

        private Button GetButton(string text)
        {
            Button button = new Button();
            button.Content = text;
            button.Width = 30;
            button.Height = 30;
            button.Style = buttonLightStyle;
            button.Margin = new Thickness(10, 10, 10, 10);
            button.HorizontalAlignment = HorizontalAlignment.Center;
            button.VerticalAlignment = VerticalAlignment.Center;

            return button;
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!optionsChoosen)
                e.Cancel = true;
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        #endregion
    }
}
